package com.njit.farm.service.impl;

import com.njit.farm.dao.FSeedMapper;
import com.njit.farm.dataobject.FSeed;
import com.njit.farm.model.Result;
import com.njit.farm.model.Seed;
import com.njit.farm.service.SeedService;
import com.njit.farm.util.IDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class SeedServiceImpl implements SeedService {

    @Resource
    FSeedMapper fSeedMapper;

    @Override
    public Result add(Seed seed) {

        Result result = new Result();

        FSeed fSeed = new FSeed();
        fSeed.setsId(IDUtils.uuid());
        fSeed.setsGrowDay(seed.getGrowDay());
        fSeed.setsGuessValue(seed.getGuessValue());
        fSeed.setsName(seed.getName());
        fSeed.setsPrice(seed.getPrice());
        fSeed.setsPicUrl(seed.getPicUrl());

        fSeedMapper.insert(fSeed);

        result.setSucc(true);
        return result;
    }

    @Override
    public Result get(String sid) {

        Result result = new Result();
        FSeed fSeed = fSeedMapper.selectByPrimaryKey(sid);

        result.setSucc(true);
        result.setData(fSeed.toModel());

        return result;
    }

    @Override
    public Result getAll() {

        Result result = new Result();

        List<FSeed> fSeeds = fSeedMapper.selectAll();

        List<Seed> seedList = new ArrayList<>();
        for (FSeed fSeed : fSeeds) {
            seedList.add(fSeed.toModel());
        }

        result.setSucc(true);
        result.setData(seedList);
        return result;
    }
}
